package com.nut.utils;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

public class uploadImgUtils {
	public static final String IMGSERVER = "http://172.18.0.27/";

	public static String upload(MultipartFile logoImg) throws FileNotFoundException, IOException, Exception {
		String result = "";

		ClassPathResource resource = new ClassPathResource("fdfs_client.conf");
		// 获取资源配置文件
		ClientGlobal.init(resource.getClassLoader().getResource("fdfs_client.conf").getPath());
		// 客户端
		TrackerClient trackerClient = new TrackerClient();
		// 服务端 客户端连接到服务端
		TrackerServer trackerServer = trackerClient.getConnection();
		// 服务端返回一个storage的地址
		StorageClient1 storageClient = new StorageClient1(trackerServer, null);
		// 三个参数： 文件字节流、文件扩展名、文件信息
		result = storageClient.upload_file1(logoImg.getBytes(),
				FilenameUtils.getExtension(logoImg.getOriginalFilename()), null);

		return result;
	}
}
